package com.botao.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.botao.cms.pojo.Log;
import com.botao.cms.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@Controller
public class LogController {
    @Autowired
    LogService logService;

    @GetMapping(path = {"admin/sys/log"})
    public String userAdd(Model model, Long currentPage, Long pageSize) {
        Page<Log> page = new Page<>(currentPage, pageSize);
        QueryWrapper<Log> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("created");
        Page<Log> logPage = logService.page(page, wrapper);
        long total = logPage.getTotal();
        long current = logPage.getCurrent();
        boolean hasNext = logPage.hasNext();
        List<Log> logList = logPage.getRecords();
        model.addAttribute("logList", logList);
        model.addAttribute("total", total);
        model.addAttribute("current", current);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("hasNext", hasNext);
        return "admin/sys/log";
    }


    @GetMapping("admin/sys/log/export")
    public void export(HttpServletResponse response) throws IOException {
        logService.exportLog(response);
    }
}
